import { openDB, initDBAll } from './openDB';
import { storeName as infoStoreName } from './info.js'
import { storeName as menoStoreName } from './meon.js'
import { storeName as logStoreName } from './log.js'

const dbName = 'infoDB';
const version = 12;

// 格式化返回数据
export function resultFomat(data, key = 'data', msg = '', code = 200) {
  return {
    code: code,
    [key]: data,
    msg,
  };
}


// random 创建主键id
export function randomId() {
  return Date.now() + Math.random().toString(16).slice(2, 10);
}



// 创建db对象
export async function createDB(storeName) {
  return await openDB(dbName, storeName, version);
}

// 创建时间
export function createTime() {
  const date = new Date().toLocaleDateString();
  const time = new Date().toLocaleTimeString();
  const [y, m, d] = date.split('/');
  const mm = m > 9 ? m : `0${m}`;
  const dd = d > 9 ? d : `0${d}`;
  return `${y}-${mm}-${dd} ${time}`;
}



// 初始化所有表 db
export async function initAllDB() {
  await initDBAll(dbName, [infoStoreName,menoStoreName,logStoreName], version);
}